<template>
  <div class="home-container">
    <!-- 导航栏 原本只能通过title属性设置一个导航栏的标题，现在添加一个搜索按钮，需要插槽-->
    <!-- 这样的效果就是将原本标题的位置，换成一个按钮进行显示 -->
    <van-nav-bar  class="ba" fixed>
        <!--增加一个名字为title的插槽 -->
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/seach"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- tab栏 -->
    <van-tabs v-model="active" class="channel-tabs" swipeable animated>
  <van-tab v-for="va in list" :key="va.id" :title="va.name">
<keep-alive>
<LisComponent :channel='va'></LisComponent>
</keep-alive>
  </van-tab>

  <div slot="nav-right" class ='placeholder'>  </div>
  <div slot="nav-right" @click="showPopup" class ='hamburger-btn' >
    <i class="toutiao toutiao-gengduo"></i>
  </div>
</van-tabs>
<!-- 弹出层 -->

<van-popup v-model="show"
 round position="bottom"
 closeable
:style="{ height: '100%' }" >

<ChannelEdit @vaice='ask'
:active="active"
 :my-channels='list'>

</ChannelEdit>
</van-popup>
  </div>
</template>

<script>
// 引入列表组件
import LisComponent from './cons/index.vue'
import ChannelEdit from './cons/channel-edit'
import { pindao } from '@/api/user'
// import{getUserChannels} from '@/api/channel.js'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    LisComponent,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      //  接收频道列表
      list: [],
      // 弹出层
      show: false
    }
  },
  computed: {

    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.load()
  },
  mounted () {},
  methods: {
    // 弹出层
    showPopup () {
      this.show = true
    },
    async load () {
      try {
        // 获取频道列表

        if (this.user) {
          // 意境登录
          const res = await pindao()
          // console.log(res.data.data.channels);
          this.list = res.data.data.channels
        } else {
          // 未登录
          // 获取本地储存列表
          const local = getItem('TOUTIAO_CHANNELS')
          if (local) {
            this.list = local
          } else {
            // 没有本地频道数据，则请求获取默认推荐的频道列表
            const res = await pindao()
            // console.log(res.data.data.channels);
            this.list = res.data.data.channels
          }
        }
      } catch (error) {
        console.log(error)
        this.$toast('获取频道失败')
      }
    },
    ask (index, show = true) {
      console.log(111)
      this.active = index
      this.show = show
    }

  }
}
</script>

<style scoped lang="less">

.home-container {
 padding-top: 174px;
    padding-bottom: 100px;

  /deep/ .van-nav-bar__title {

    max-width: unset;
  }
  /deep/.van-tabs__wrap{
    position: fixed;
  z-index: 99;
  left: 0;
  right: 0;
  top:92px}
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}

/deep/ .channel-tabs {
    .van-tabs__wrap {   //这里只需要给父节点channel-tabs添加/deep/,这样下边的子节点都会生效
      height: 82px;
    }

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
}
.placeholder {//占位元素为了解决最后一个tab标签被汉堡盖住问题（看下边的图）
  flex-shrink: 0;//此元素不参与flex的
  width: 66px;
  height: 82px;
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    font-size: 33px;
  }
  &:before { //找到父节点，添加伪元素（这个是汉堡按钮与tab之间的一个竖线）
    content: "";
    position: absolute;
    left: 0;
    width: 1px;//在模拟器上看不到（手机上没问题），如果想看到改为2px
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
</style>
